<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of account
 *
 * @author lz06
 */
class AccountMoneyDb {

    private $dbc;

    function __construct(DBC $dbc) {
        $this->dbc = $dbc;
    }

    /**
     * 根据用户名，获得余额
     * @param type $accountId
     * @return type
     */
    public function getAccountMoney($accUnique) {
        $sql = "SELECT * FROM AccountMoney WHERE AccUnique = '" . $accUnique . "'";
        $data = $this->dbc->fetchData($sql);
        if (!isArrayNotEmpty($data)) {
            $sql = "INSERT INTO AccountMoney(AccUnique,Balance,UpdateTime) VALUES('" . $accUnique . "',0,getDate())";
            if ($this->dbc->execute($sql)) {
                $sql = "SELECT * FROM AccountMoney WHERE AccUnique = '" . $accUnique . "'";
                $data = $this->dbc->fetchData($sql);
            }
        }
        return $data;
    }

    /**
     * 更新余额
     * @param type $accUnique
     * @param type $accountId
     * @param type $money
     * @return type
     */
    public function updateAccountMoney($accUnique, $accountId, $money) {
        $sql = "UPDATE AccountMoney SET accountpass = '" . $newPassword . "' WHERE AccUnique = '" . $accUnique . "' AND AccountID = '" . $accountId . "' AND accountpass = '" . $oldPassword . "'";
        return $this->dbc->execute($sql);
    }
    
    public function setAccountMoneyZero($accUnique){
        $sql = "INSERT INTO AccountMoney(AccUnique,Balance,UpdateTime) VALUES('".$accUnique."',0,getDate());";
        return $this->dbc->execute($sql);
    }

    /**
     * 管理员赠送金币
     * @param type $accUnique
     * @param type $gold
     * @return type
     */
    public function sendAccountMoney($accUnique, $gold) {
        $sqls = array();
        $sql = "UPDATE AccountMoney SET Balance = Balance + " . $gold . " where AccUnique = " . $accUnique;
        array_push($sqls, $sql);
        $unionNo = date("YmdHis") . "_" . $accUnique;
        $sql = "INSERT INTO AccountMoneyDetail(AccUnique,ItemIdx,ItemCount,UpdateTime,ItemNo,UnionNo) values('"
                . $accUnique . "',-1,1,getDate(),'','" . $unionNo . "')";
        array_push($sqls, $sql);
        return $this->dbc->executeBatch($sqls);
    }

    /**
     * 用户间赠送金币
     * @param type $accUnique
     * @param type $gold
     * @return type
     */
    public function giftAccountMoney($accUnique, $otherAccUnique, $gold) {
        $sqls = array();
        $sql = "SELECT Balance FROM AccountMoney where AccUnique = " . $accUnique;
        $old = $this->dbc->fetchData($gold);
        if (isArrayNotEmpty($old)) {
            if ($old[0]['Balance'] < $gold) {
                return false;
            }
        }
        $sql = "UPDATE AccountMoney SET Balance = Balance - " . $gold . " where AccUnique = " . $accUnique;
        array_push($sqls, $sql);
        $unionNo = date("YmdHis") . "_" . $accUnique;
        $sql = "INSERT INTO AccountMoneyDetail(AccUnique,ItemIdx,ItemCount,UpdateTime,ItemNo,UnionNo) values('"
                . $accUnique . "',-1,1,getDate(),'','" . $unionNo . "')";
        array_push($sqls, $sql);

        $sql = "UPDATE AccountMoney SET Balance = Balance + " . $gold . " where AccUnique = " . $otherAccUnique;
        array_push($sqls, $sql);
        $unionNo = date("YmdHis") . "_" . $otherAccUnique;
        $sql = "INSERT INTO AccountMoneyDetail(AccUnique,ItemIdx,ItemCount,UpdateTime,ItemNo,UnionNo) values('"
                . $otherAccUnique . "',-1,1,getDate(),'','" . $unionNo . "')";
        array_push($sqls, $sql);        
        return $this->dbc->executeBatch($sqls);
    }

}
